#include "notedb.h"

#include <QSqlError>
#include <QSqlQuery>
#include <QSqlRecord>

NoteDB* NoteDB::instance = nullptr;

void NoteDB::insert(ImageInfo &info)
{
    qDebug() << "do insert ";
    QSqlQuery query = getInstance()->getQuery();
    query.prepare("INSERT INTO info VALUES(?,?,?,?,?,?)");
    int i(0);
    query.bindValue(i++, info.id);
    query.bindValue(i++, info.imgFileName);
    query.bindValue(i++, info.size.width);
    query.bindValue(i++, info.size.height);
    query.bindValue(i++, info.position.top);
    query.bindValue(i++, info.position.left);
    query.exec();
}

void NoteDB::deleteOne(QString id)
{
    qDebug() << "do delete";
    QSqlQuery query = getInstance()->getQuery();
    query.prepare("DELETE FROM info WHERE id=?");
    query.bindValue(0, id);
    query.exec();

}

void NoteDB::deleteAll()
{

}

void NoteDB::update(ImageInfo info)
{
    qDebug() << "do update";
    QSqlQuery query = getInstance()->getQuery();
    query.prepare("UPDATE info SET imgFileName=?,width=?,height=?,top=?,left=? WHERE id=?");
    int i(0);
    query.bindValue(i++, info.imgFileName);
    query.bindValue(i++, info.size.width);
    query.bindValue(i++, info.size.height);
    query.bindValue(i++, info.position.top);
    query.bindValue(i++, info.position.left);
    query.bindValue(i++, info.id);
    query.exec();
}

void NoteDB::insertOrUpdate(ImageInfo &info)
{
    if(exist(info.id)){
        update(info);
    }else{
        insert(info);
    }
}

bool NoteDB::exist(QString id)
{
    QSqlQuery query = getInstance()->getQuery();
    query.prepare("SELECT id FROM info WHERE id=?");
    query.bindValue(0, id);
    query.exec();
    query.next();
    QSqlRecord record = query.record();
//    qDebug() << "record:" << record.value("id").toString();
//    qDebug() << "exist: " << !record.isEmpty() << " isValid:" << query.isValid();

    return query.isValid();
}

QSqlQuery NoteDB::findAll()
{
    QSqlQuery query = getInstance()->getQuery();
    query.exec("SELECT * FROM info");

    return query;

}

void NoteDB::closeDB()
{
    getInstance()->close();
}

NoteDB::NoteDB()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("note.dat");

    const char* sql = "CREATE TABLE IF NOT EXISTS info("
                      "id TEXT CONSTRAINT KEY,"
                      "imgFileName TEXT,"
                      "width INT,"
                      "height INT,"
                      "top INT,"
                      "left INT"
                      ")";

    db.open();
    QSqlQuery query(db);
    auto b = query.exec(sql);

    if(query.lastError().isValid()){
        qDebug() << query.lastError().type() << " " << query.lastError().text();
    }

    qDebug() << "NoteDB::NoteDB query.exec:" << b;
}

NoteDB::~NoteDB()
{
    NoteDB::getInstance()->db.close();
}

QSqlQuery NoteDB::getQuery()
{
    this->db.open();
    return QSqlQuery(this->db);
}

bool NoteDB::open()
{
    return db.open();
}

void NoteDB::close()
{
    db.close();
    qDebug() << "db close.";
}

NoteDB *NoteDB::getInstance()
{
    if(instance == nullptr){
        instance = new NoteDB();
    }

    return instance;
}


